﻿using System;
using System.Runtime.Serialization;
using System.Text;

namespace PasswordProvider.ObjectModel
{
    /// <summary>
    /// LoginSite encapsulates a site, typically a web site, which requires login and password data to access.
    /// This class stores the data necessary to identify, navigate to and log in to the site.
    /// </summary>
    [DataContract]
    public class LogOnSite : Site
    {
        public LogOnSite() { }

        public LogOnSite(string name, string description)
            : base(name, description)
        {
        }
        public LogOnSite(string name, string description, Uri url, string logOnId)
            : base(name, description)
        {
            _url = url;
            _logOnId = logOnId;
        }

        #region Public Data Members

        [DataMember]
        public string LogOnId 
        {
            get { return _logOnId; }
            set { SetPropertyValue(value, ref _logOnId, "LogOnId"); }
        } private string _logOnId;

        [DataMember]
        public Uri Url 
        {
            get { return _url; }
            set { SetPropertyValue(value, ref _url, "Url"); }
        } private Uri _url;

        [DataMember]
        public Uri IconUrl 
        {
            get { return _iconUrl; }
            set { SetPropertyValue(value, ref _iconUrl, "IconUrl"); }
        } private Uri _iconUrl; 
        
        [DataMember]        
        public EncryptedValue EncryptedPassword 
        {
            get 
            {
                if (_encryptedPassword == null)
                    EncryptedPassword = new EncryptedValue();
                return _encryptedPassword; 
            }
            private set 
            {
                _encryptedPassword = value;
                if(_encryptedPassword != null)
                    _encryptedPassword.PropertyChanged += delegate { OnPropertyChanged("EncryptedPassword"); };
            }            
        } private EncryptedValue _encryptedPassword;

        #endregion

        #region Overrides

        public override EncryptedValue PrimaryEncryptedValue
        {
            get
            {
                return EncryptedPassword;
            }
        }

        internal override Site Clone()
        {
            LogOnSite clone = new LogOnSite();
            clone.Copy(this);
            return clone;
        }

        internal override void Copy(Site site)
        {
            base.Copy(site);
            LogOnSite loginSite = site as LogOnSite;
            EncryptedPassword = loginSite.EncryptedPassword;
            LogOnId = loginSite.LogOnId;
        }

        internal override string WriteExportString(SecurityManager securityManager, bool decryptAll)
        {
            StringBuilder builder = new StringBuilder(Name);
            builder.Append(" (");
            builder.Append(LogOnId);
            builder.Append(")\nPW: ");
            builder.Append(decryptAll ? securityManager.GetValueDecrypted(EncryptedPassword, false).UnprotectedString : "************");
            builder.Append("\nUrl: ");
            builder.Append(Url);
            builder.Append("\nDescription: ");
            builder.Append(Description);

            //builder.Append(",");
            //builder.Append(LogOnId);
            //builder.Append(",");
            //builder.Append(decryptAll ? securityManager.GetValueDecrypted(EncryptedPassword, false).UnprotectedString : "************");
            //builder.Append(",");
            //builder.Append(Url);
            //builder.Append(",");
            //builder.Append(Description);

            builder.Append("\n");

            return builder.ToString();
        }

        #endregion // Overrides
        
    }
}
